package com.huonan.component.tmp;

import com.huonan.component.tmp.vo.UserLanguageTextVo;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则工具类
 */
public class RegularUtil {

    public static void extractAndRemoveMatches(UserLanguageTextVo userLanguageTextVo, String regex,
                                               Consumer<List<String>> extractList) {
        if (StringUtils.isNotEmpty(regex)) {
            List<String> matches = new ArrayList<>();
            StringBuffer output = new StringBuffer();

            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(userLanguageTextVo.getAfterText());
            while (matcher.find()) {
                String match = matcher.group(0);
                matches.add(match);
                matcher.appendReplacement(output, "");
            }
            extractList.accept(matches);
            matcher.appendTail(output);
            userLanguageTextVo.setAfterText(output.toString());
        }
    }

    public static void rmMatche(List<String> emojiList, UserLanguageTextVo userLanguageTextVo) {
        String regex = String.join("|", emojiList);
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(userLanguageTextVo.getAfterText());
        userLanguageTextVo.setAfterText(matcher.replaceAll(""));
    }
}
